/*
    SPDX-FileCopyrightText: MOS Team <https://os.mos.ru>
    SPDX-FileCopyrightText: 2024 Sergey Katunin <sulmpx60@yandex.ru>

    SPDX-License-Identifier: BSD-2-Clause
*/

import QtQuick 2.0
import QtQuick.Layouts 1.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import QtQuick.Controls 2.15 as Controls
import org.kde.kirigami 2.20 as Kirigami
import org.kde.plasma.plasmoid 2.0

Item {
    id: root

    property var cmds: {
        "GetHostname": "hostname -f",
        "GetIp": " hostname -I | tr ' ' '\n'",
        "GetSshStatus": "systemctl is-active sshd",
        "GetKrfbStatus": "pgrep krfb -l && ss -tnlp | grep krfb",
    }

    property string areaText: "/// Информация для СТП ///\n\n" + "Имя ПК: " + hostname + "\n\n" + "IP: "
                              + ip + "\n\n" + sshStatus + "\n" + krfbStatus;
    property string hostname: "";
    property string ip: "";
    property string sshStatus: "";
    property string krfbStatus: "";

    property int busyCounter: 0;
    readonly property bool busy: busyCounter != 0;

    PlasmaCore.DataSource {
        id: dataSourceExec
        engine: "executable"
        onNewData: {
            let exitCode = data["exit code"];
            let exitStatus = data["exit status"];
            let stdout = data["stdout"];

            switch (sourceName) {
            case cmds["GetHostname"]:
                hostname = stdout.trim();
                break;
            case cmds["GetIp"]:
                ip = stdout.trim();
                break;
            case cmds["GetSshStatus"]:
                sshStatus = stdout.trim() === "active" ? "Служба SSH активна [✅]." : "Служба SSH неактивна [❌].";
                break;
            case cmds["GetKrfbStatus"]:
                krfbStatus = exitCode === 0 ? "Сервер Krfb запущен [✅]." : "Сервер Krfb не запущен [❌].";
                break;
            }

            disconnectSource(sourceName) // cmd finished
            --busyCounter;
        }

        function exec(cmd) {
            connectSource(cmd)
        }
    }

    Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation

    Plasmoid.fullRepresentation: ColumnLayout {
        Layout.preferredWidth: 400

        ColumnLayout {
            visible: busy
            PlasmaComponents.Label {
                id: test
                font.pointSize: 12
                text: "Пожалуйста, подождите..."
                Layout.fillWidth: true
            }

            Controls.ProgressBar {
                from: 0
                to: 4
                value: (4 - busyCounter)
                indeterminate: false
                Layout.fillWidth: true
            }
        }

        PlasmaComponents.TextArea {
            id: area
            font.pointSize: 14
            readOnly: true
            text: areaText
            visible: !busy

            Layout.fillWidth: true
            Layout.preferredHeight: 240
        }

        Kirigami.InlineMessage {
            id: label
            text: "Уже скопировано в буфер обмена!"

            visible: !busy
            Layout.fillWidth: true

            actions: Kirigami.Action {
                icon.name: "window-close"
                onTriggered: {
                    Plasmoid.expanded = false;
                }
            }
        }

        Plasmoid.onExpandedChanged: {
            if (Plasmoid.expanded) {
                busyCounter = 4;
                dataSourceExec.exec(cmds["GetHostname"]);
                dataSourceExec.exec(cmds["GetIp"]);
                dataSourceExec.exec(cmds["GetSshStatus"]);
                dataSourceExec.exec(cmds["GetKrfbStatus"]);
            }
            else {
                busyCounter = 0;
            }
        }

        Connections {
            target: root
            function onBusyChanged() {
                if (!busy && Plasmoid.expanded) {
                    area.selectAll();
                    area.copy();
                }
            }
        }
    }
}
